/*
 * @Author: zxc
 * @Date: 2020-11-14 10:17:29
 * @LastEditTime: 2020-11-14 10:47:30
 * @LastEditors: zxc
 * @Description: 组件模式 => 它是一种聚合关系
 */

/**
 * @Author: zxc
 * @Date: 2020-11-14 10:17:41
 * @func 扫描文件夹的例子
 */
// class Folder {
//   constructor(name) {
//     this.name = name
//     this.files = []
//   }
//   add(file) {
//     this.files.push(file)
//   }
//   scan() {
//     console.log("开始扫描文件夹", this.name)
//     for (var i = 0; i < this.files.length; i++) {
//       this.files[i].scan()
//     }
//   }
// }
// class File {
//   constructor(name) {
//     this.name = name
//   }
//   add() {
//     throw new Error("操作错误，文件已是最底层结点")
//   }
//   scan() {
//     console.log("开始扫描文件", this.name)
//   }
// }

// let sutyFloder = new Folder("学习资料")
// let javascript = new Folder("javascript")
// let webpack = new Folder("webpack")

// let jsForMdn = new File("jsForMdn")
// let jsForW3C = new File("jsForW3C")
// let webpackForZh = new File("webpackForZh")

// javascript.add(jsForMdn)
// webpack.add(webpackForZh)

// sutyFloder.add(javascript)
// sutyFloder.add(webpack)
// sutyFloder.add(jsForW3C)

// let node = new Folder("nodejs")
// let nodeForZh = new File("nodeForZh")
// node.add(nodeForZh)

// let ruby = new File("ruby")

// sutyFloder.add(node)
// sutyFloder.add(ruby)

// sutyFloder.scan()

/**
 * @Author: zxc
 * @Date: 2020-11-14 10:33:00
 * @func 还是扫描文件夹的例子->改写一下
 */

class Folder {
  constructor(name) {
    this.name = name
    this.parent = null
    this.files = []
  }
  add(file) {
    file.parent = this
    this.files.push(file)
  }
  remove() {
    if (!this.parent) return
    let files = this.parent.files
    for (var i = files.length - 1; i > 0; i--) {
      if (files[i] === this) {
        files.splice(i, 1)
        break
      }
    }
  }
  scan() {
    console.log("开始扫描文件夹", this.name)
    for (var i = 0; i < this.files.length; i++) {
      this.files[i].scan()
    }
  }
}
class File {
  constructor(name) {
    this.name = name
    this.parent = null
  }
  add() {
    throw new Error("操作错误，文件已是最底层结点")
  }
  remove() {
    if (!this.parent) return
    let files = this.parent.files
    for (var i = files.length - 1; i >= 0; i--) {
      if (files[i] === this) {
        files.splice(i, 1)
        break
      }
    }
  }
  scan() {
    console.log("开始扫描文件", this.name)
  }
}

let sutyFloder = new Folder("学习资料")
let javascript = new Folder("javascript")
let webpack = new Folder("webpack")

let jsForMdn = new File("jsForMdn")
let jsForW3C = new File("jsForW3C")
let webpackForZh = new File("webpackForZh")

javascript.add(jsForMdn)
webpack.add(webpackForZh)

sutyFloder.add(javascript)
sutyFloder.add(webpack)
sutyFloder.add(jsForW3C)

webpackForZh.remove()

sutyFloder.scan()
